#!/usr/bin/perl -w
use 5.010;

$BUFFER_SIZE = 5;
@add = qw(
	10.20.79.1
	10.20.66.2
	10.20.85.2
);
#
#read file to memory
#
for (my $i = 0; $i < @add; $i++)
{
	open IN_FILE, "<$add[$i].index.backup"
		or die "Cannot open file to read: $!";
	
	while (<IN_FILE>)
	{
		my @para = split /\/\//, $_;
		chomp ($t = pop @para);
		$s = pop @para;
		$p = pop @para;
		$n = "@para";	
		push @{ $record[$i] }, { Time => "$t", Size => "$s", Path => "$p", Name => "$n" };
	}
	close IN_FILE;
}
#
#accept user input
#
say "type ? for more information about FTP search software :)";
while (1)
{
	my $count = 0;
	my $size_check = 0;
	my $time_check = 0;
	my $format_check = 1;
	$min_size = $max_size = $cmp_s = undef;
	$cmp_t = $year1 = $month1 = $day1 = $year2 = $month2 = $day2 = undef;

	print "search: ";
	my $cmd;
	$cmd = <>;
	if (!$cmd)#exit program when user press Ctrl+C
	{
		exit ;
	}
	chomp($cmd);
	if ($cmd eq '?')
	{
		print "This is a bugy software, enjoy it...\n";
		next;
	}
	my @para = split /\s+/, $cmd;
	if (@para > 3 || @para <= 0)
	{
		say "invaild input";
		next;
	}
	for (my $i = 1; $i < @para; $i++)
	{
		my $t = substr($para[$i], 0, 3);
		if ($t eq '-t:')
		{
			if (&time_filter(substr($para[$i], 3)))
			{
				$time_check = 1;
			}
			else
			{
				say "invalid time filter";
				$format_check = 0;
			}
		}
		elsif ($t eq '-s:')
		{
			if (&size_filter(substr($para[$i], 3)))
			{
				$size_check = 1;
			}
			else
			{
				say "invalid size filter";
				$format_check = 0;
			}
		}
		else
		{
			next;
		}
	}
	if (!$format_check)
	{
		next;
	}
	for (my $i = 0; $i < @add; $i++)
	{
		push @buffer, "\nSite $add[$i]:";
		foreach $line (@{ $record[$i] })
		{
			if ($line->{Name} =~ /($para[0])/i)
			{
				my $flag = 1;
				if ($size_check == 1)
				{
					if (($cmp_s eq '-' && ($line->{Size}<=$min_size || $line->{Size}>=$max_size))
						|| ($cmp_s eq '<' && $line->{Size} >= $min_size)
						|| ($cmp_s eq '>' && $line->{Size} <= $min_size)
						|| ($cmp_s eq '>=' && $line->{Size} < $min_size)
						|| ($cmp_s eq '<=' && $line->{Size} > $min_size))
					{
						$flag = 0;
					}
				}
				if ($time_check == 1)
				{
					my ($d, $m, $y) = split /\//, $line->{Time};
					if (($cmp_t eq '-' && &time_cmp('>', $d, $m, $y, $day1, $month1, $year1) && &time_cmp('<', $d, $m, $y, $day2, $month2, $year2))
						|| ($cmp_t eq '>' && &time_cmp('<=', $d, $m, $y, $day1, $month1, $year1))
						|| ($cmp_t eq '<' && &time_cmp('>=', $d, $m, $y, $day1, $month1, $year1))
						|| ($cmp_t eq '>=' && &time_cmp('<', $d, $m, $y, $day1, $month1, $year1))
						|| ($cmp_t eq '<=' && &time_cmp('>', $d, $m, $y, $day1, $month1, $year1)))
					{
						$flag = 0;
					}
				}
				if ($flag == 1)
				{
					push @buffer, "$line->{Name}\n\tPath:$line->{Path}\n\tSize:$line->{Size}\n\tModTime:$line->{Time}";
					$count++;
				}
			}
		}
	}
	#
	#output search result to users
	#
	my $end = 0;
	my $num = $BUFFER_SIZE;
	say "\nTOTAL found: $count";
	while (@buffer > 0)
	{
		$num = @buffer < $BUFFER_SIZE ? @buffer : $BUFFER_SIZE;
		for (my $i=0; $i<$num; $i++)
		{
			say shift @buffer;
		}
		say "<press ENTER to display more results>";
		$cmd = <>;
		chomp($cmd);
		if ($cmd ne '')
		{
			@buffer = ();
			last;
		}
	}
}

sub time_cmp
{
	my ($cmp, $d, $m, $y, $dd, $mm, $yy) = ($_[0], $_[1], $_[2], $_[3], $_[4], $_[5], $_[6]);
	if ($cmp eq '>')
	{
		if ($y > $yy) {return 1;}
		if ($y < $yy) {return 0;}
		if ($m > $mm) {return 1;}
		if ($m < $mm) {return 0;}
		if ($d > $dd) {return 1;}
		return 0;
	}
	elsif($cmp eq '<')
	{
		if ($y < $yy) {return 1;}
		if ($y > $yy) {return 0;}
		if ($m < $mm) {return 1;}
		if ($m > $mm) {return 0;}
		if ($d < $dd) {return 1;}
		return 0;
	}
	elsif($cmp eq '>=')
	{
		if ($y > $yy) {return 1;}
		if ($y < $yy) {return 0;}
		if ($m > $mm) {return 1;}
		if ($m < $mm) {return 0;}
		if ($d >= $dd) {return 1;}
		return 0;
	}
	else
	{
		if ($y < $yy) {return 1;}
		if ($y > $yy) {return 0;}
		if ($m < $mm) {return 1;}
		if ($m > $mm) {return 0;}
		if ($d <= $dd) {return 1;}
		return 0;
	}
}
sub time_filter
{
	my ($y1, $m1, $d1, $y2, $m2, $d2, $cmp);	
	if (($y1, $m1, $d1, $y2, $m2, $d2) = $_[0] =~ /(\d{4})\/(\d{1,2})\/(\d{1,2})[-,](\d{4})\/(\d{1,2})\/(\d{1,2})/)
	{
		if (&time_cmp('<=', $y1, $m1, $d1, $y2, $m2, $d2) == 0)
		{
			($y1, $y2) = ($y2, $y1);
			($m1, $m2) = ($m2, $m1);
			($d1, $d2) = ($d2, $d1);
		}
		$year1 = $y1;
		$month1 = $m1;
		$day1 = $d1;
		$year2 = $y2;
		$month2 = $m2;
		$day2 = $d2;
		$cmp_t = '-';
	}
	elsif (($cmp, $y1, $m1, $d1) = $_[0] =~ /(>|<|=|<=|>=)(\d{4})\/(\d{1,2})\/(\d{1,2})/)
	{
		$year1 = $y1;
		$month1 = $m1;
		$day1 = $d1;
		$cmp_t = $cmp;
	}
	else
	{
		return 0;
	}
	return 1;
}

sub size_filter
{
	my ($cmp, $n1, $unit, $n2);

	if (($n1, $n2, $unit) = $_[0] =~ /(\d+)[-,](\d+)([gkmb]?)/)
	{
		if ($unit ne 'b' && $unit)
		{
			if ($unit eq 'g') {$n1 *= 1073741824; $n2 *= 1073741824;}
			elsif ($unit eq 'm') {$n1 *= 1048576; $n2 *= 1048576;}
			else {$n1 *= 1024; $n2 *= 1024;}
		}
		$min_size = $n1;
		$max_size = $n2;
		$cmp_s = '-';
	}
	elsif (($cmp, $n1, $unit) = $_[0] =~  /(>|<|>=|<=)(\d+)([gkmb]?)/)
	{
		if ($unit ne 'b' && $unit)
		{
			if ($unit eq 'g') {$n1 *= 1073741824;}
			elsif ($unit eq 'm') {$n1 *= 1048576;}
			else {$n1 *= 1024;}
		}
		$min_size = $n1;
		$cmp_s = $cmp;
	}
	else
	{
		return 0;
	}
	return 1;
}
